<template>
  <el-dialog
    top="30px"
    width="700px"
    append-to-body
    title="上传推荐函"
    :close-on-click-modal="false"
    :before-close="beforeClose"
    :visible.sync="visible"
  >
    <upload-oss
      multiple
      :dir="dir"
      auto-upload
      :file-name="0"
      class="form-control-ml"
      ref="letterUpload"
      :file-list="letterList"
      :on-complete="letterComplete"
    ></upload-oss>
  </el-dialog>
</template>

<script>
import { arrayToString } from '@vimi/utils-tool'
export default {
  components: {},
  props: {
    visible: Boolean,
    recommendData: {
      type: Array,
      default() {
        return []
      },
    },
  },
  data() {
    return {
      tableName: 'credit_industrial',
      letterList: [],
    }
  },
  computed: {
    dir() {
      return `gov/recommend/${+new Date()}`
    },
    recommendId() {
      return this.recommendData.map(item => item.credit_industrial_id)
    },
  },
  watch: {},
  created() {},
  mounted() {},
  methods: {
    letterComplete(files) {
      const url = arrayToString(files.map(it => it.url))
      this.updateData(url)
    },
    updateData(url) {
      const updateList = this.recommendId.map(item => {
        return {
          credit_industrial_id: item,
          is_recommend_letter: 1,
          recommend_letter_attachment: url,
        }
      })
      this.$api
        .updateBatch(this.$serviceName, this.tableName, updateList)
        .then(() => {
          this.$message.success('上传成功')
          this.beforeClose()
        })
        .finally(() => {
          this.$emit('success', true)
        })
    },
    beforeClose() {
      this.$emit('update:visible', false)
    },
  },
}
</script>

<style lang="scss" scoped></style>
